package com.xm;

import java.util.Arrays;

public class Array04 {
    public static void main(String[] args) {

        // 定义数组
        int[] test = new int[3];
        System.out.println(Arrays.toString(test));
        // int[] test = new int[];编译报错
        int[] test2 = {1, 2};
        System.out.println(Arrays.toString(test2));
        int[] test3 = {};
        System.out.println(Arrays.toString(test3));
        int[] test4 = new int[]{};
        System.out.println(Arrays.toString(test4));
        int[] test5 = new int[]{1};
        System.out.println(Arrays.toString(test5));

        int size = 3;
        int[] arr = new int[size];
        System.out.println(Arrays.toString(arr));
        arr[0] = 1;
        arr[1] = 2;
        arr[2] = 3;
        System.out.println(Arrays.toString(arr));

        int total = 0;
        for (int i = 0; i < size; i++) {
            total += arr[i];
        }
        System.out.println("total = " + total);

        System.out.println("---分割线---");
        int[] arr2 = {2, 4, 6};

        // 打印所有数组元素
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }

        // 计算所有元素的总和
        int total2 = 0;
        for (int i = 0; i < arr2.length; i++) {
            total2 += arr2[i];
        }
        System.out.println("total2 = " + total2);

        // 查找最大元素
        int max = arr2[0];
        for (int i = 0; i < arr2.length; i++) {
            if (max < arr2[i]) {
                max = arr2[i];
            }
        }
        System.out.println("max = " + max);

        // For-Each 循环或者叫加强型循环
        for (int i : arr2) {
            System.out.println("i = " + i);
        }

        for (int i : arr2) {
            if (max < i) {
                max = i;
            }
        }
        System.out.println("max333 = " + max);

        // 数组作为函数的参数
        printArray(new int[]{3, 2});

        // 数组作为函数的返回值
        reverse(new int[]{4, 3, 2, 1});

    }

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);

        }
    }

    private static int[] reverse(int[] list) {
        int[] result = new int[list.length];
        for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
            result[j] = list[i];
        }
        System.out.println(Arrays.toString(result));
        return result;
    }
}
